package com.eventapp.service.impl;

import com.eventapp.dao.EventDao;
import com.eventapp.dao.RoleDao;
import com.eventapp.dao.UserDao;
import com.eventapp.model.Event;
import com.eventapp.model.EventPrivate;
import com.eventapp.model.EventPublic;
import com.eventapp.model.Role;
import com.eventapp.model.User;
import com.eventapp.service.EventExistsException;
import com.eventapp.service.EventManager;
import com.eventapp.service.EventService;
import com.eventapp.service.RoleManager;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

import javax.jws.WebService;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;


@Service("eventManager")
@WebService(serviceName = "EventService", endpointInterface = "com.eventapp.service.EventService")
public class EventManagerImpl extends GenericManagerImpl<Event, Long> implements EventManager,EventService {
	
	@Autowired
	EventDao eventDao;

	@Autowired
	UserDao userDao;
	
	@Override
	public Event getEvent(String eventId) {
		return eventDao.get(Long.valueOf(eventId));
	}
	@Override
	public Event saveEvent(Event event) throws EventExistsException{
		return eventDao.saveEvent(event);
	}

	public Response createEventPublic(String json) throws EventExistsException, JsonParseException, JsonMappingException, IOException{
		ObjectMapper mapper = new ObjectMapper();
		EventPublic  entity = mapper.readValue(json, EventPublic.class);
		entity.setOwner(userDao.get(entity.getOwner().getId()));
		entity = (EventPublic) this.saveEvent(entity);
		return Response.status(Status.OK).entity(entity).build();
	}

	public Response createEventPrivate(String json) throws EventExistsException, JsonParseException, JsonMappingException, IOException{
		ObjectMapper mapper = new ObjectMapper();
		EventPrivate  entity = mapper.readValue(json, EventPrivate.class);
		entity.setOwner(userDao.get(entity.getOwner().getId()));
		entity = (EventPrivate) this.saveEvent(entity);
		return Response.status(Status.OK).entity(entity).build();
	}
	
	@Override
	public void remove(Event event) {
		eventDao.remove(event);
	}

	@Override
	public List<EventPublic> getPublicEvents() {
		return  eventDao.getPublicEvents();
	}

	@Override
	public List<EventPublic> getPublicEventsByOwner(User owner) {		
		return  eventDao.getPublicEventsByOwner(owner.getId());
	}

	@Override
	public List<EventPrivate> getPrivateEventsByOwner(User owner) {
		return  eventDao.getPrivateEventsByOwner(owner.getId());
	}



	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	
	
	
	

}